package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 *@author hzl on 2022/5/16.
 *Description:
 */
@Component
@Aspect
public class AdminControllerAspect {
    @Pointcut("execution(* net.lzzy.practices.web.admin.*.*(..))")
    private void adminAspect(){}

//    @Before("adminAspect()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        joinPoint.getArgs();
//        System.out.println("方法为:" + methodName + ", 这是一个前置测试,参数为："+ joinPoint.getArgs()[0].toString());
//    }
//    @After("adminAspect()")
//    public void afterMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        joinPoint.getArgs();
//        System.out.println("方法为:" + methodName + ", 这是一个后置测试,参数为："+ joinPoint.getArgs()[0].toString());
//    }
//    @Around("adminAspect()")
//    public Object aroundMethod(ProceedingJoinPoint pjp)throws Throwable {
//         Object result=pjp.proceed(pjp.getArgs());
//         System.out.println(result.toString());
//         return result;
//    }
    @Around("adminAspect()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint)throws Throwable {
        String token =joinPoint.getArgs()[0].toString();
        if (AuthUtils.illegalUser(token,new int[]{User.TYPE_ADMIN})){
            return  new ApiResult(false, Messages.NO_PERMISSION.toString(),null);
        }
        return (ApiResult) joinPoint.proceed();
    }
}
